﻿using NthDownload.Contracts.Plugins.Downloads;
using NthDownload.Contracts;

namespace NthDownload.Common.Plugins
{
    public static class DownloadExtensions
    {
        /// <summary>
        /// Fixes the status of a download incase it has a status
        /// which indicates it is some how being processed.
        /// </summary>
        public static void FixStatus(this IDownload download, DownloadStatuses newErrorStatus, DownloadStatuses newFinishedStatus, DownloadStatuses newQueuedStatus)
        {
            if (download.Status == DownloadStatuses.Downloading)
            {
                download.Status = DownloadStatuses.Ready;
            }
            else if (download.Status == DownloadStatuses.CancelPending)
            {
                download.Status = DownloadStatuses.Canceled;
            }
            else if (download.Status == DownloadStatuses.PausePending)
            {
                download.Status = DownloadStatuses.Paused;
            }
            else if (download.Status == DownloadStatuses.StopPending)
            {
                download.Status = DownloadStatuses.Stopped;
            }
            else if (download.Status == DownloadStatuses.Error)
            {
                download.Status = newErrorStatus;
            }
            else if (download.Status == DownloadStatuses.Finished)
            {
                download.Status = newFinishedStatus;
            }
            else if (download.Status == DownloadStatuses.MassCancelPending)
            {
                download.Status = DownloadStatuses.Canceled;
            }
            else if (download.Status == DownloadStatuses.MassPausePending)
            {
                download.Status = DownloadStatuses.Paused;
            }
            else if (download.Status == DownloadStatuses.MassStopPending)
            {
                download.Status = DownloadStatuses.Stopped;
            }
            else if (download.Status == DownloadStatuses.Queued)
            {
                download.Status = newQueuedStatus;
            }
        }

        /// <summary>
        /// Resets the information recorded as being downloaded thus far for downloads that can't resumt
        /// </summary>
        /// <param name="download">The download who's downloaded info to fix</param>
        /// <param name="forceReset">Forces the data to be reset regardless of it's can resume status</param>
        public static void ResetDownloadedInfo(this IDownload download, bool forceReset)
        {
            if (download.Segments != null && (!download.CanResume || forceReset))
            {
                foreach (DownloadSegment segment in download.Segments)
                {
                    segment.CurrentPosition = segment.StartPosition;

                    if (!download.HasSize)
                    {
                        segment.EndPosition = -1;
                    }
                }
            }

            if (download.DownloadedAmount > 0 && (!download.CanResume || forceReset))
            {
                download.DownloadedAmount = 0;
            }
        }
    }
}
